package controller

import (
	"context"
	"fmt"
	"gitee.com/youbeiwuhuan/gin-practice/fizz-gorm2/initialize"
	"gitee.com/youbeiwuhuan/gin-practice/fizz-gorm2/internal/dao"
	"gitee.com/youbeiwuhuan/gin-practice/fizz-gorm2/internal/dao/entity"
	"gitee.com/youbeiwuhuan/gin-practice/fizz-gorm2/tools/logger"
	"gitee.com/youbeiwuhuan/gin-practice/fizz-gorm2/tools/render"
	"github.com/loopfz/gadgeto/tonic"
	"github.com/wI2L/fizz"
	//auths "gitee.com/youbeiwuhuan/gin-practice/gin-simple-zap/internal/web/middleware/auths"
	"github.com/gin-gonic/gin"
)

type UserInfo struct {
	Age   uint8  `json:"age" description:"年龄"  validate:"gte=1,lte=130"`
	Name  string `json:"name"  body:"name" description:"姓名"  validate:"required"`
	Email string `json:"email" description:"电子邮箱"  validate:"required,email0"`
}

type UserParam struct {
	Id uint64 `query:"id" description:"用户id"  validate:"required"`
}

type UserParam2 struct {
	Id uint64 `path:"id" description:"用户id"  validate:"required"`
}

/*
*
这里不用生成swagger.json了，直接硬编码，像写java注解一样
*/
func createUserGroup(baseGrp *fizz.Fizz) {
	userGrp := baseGrp.Group("/user", "用户管理", "用户管理模块接口" /* , auths.CheckTokenAuth() */)
	userGrp.GET("/getUser", []fizz.OperationOption{
		fizz.Summary("根据id查询用户"),
		//fizz.Response("400", "Bad request", nil, nil,
		//	map[string]interface{}{"error": "fruit already exists"},
		//),
	}, tonic.Handler(getUser, 200))

	userGrp.GET("/getUser2/:id", []fizz.OperationOption{
		fizz.Summary("根据id查询用户2"),
		//fizz.Response("400", "Bad request", nil, nil,
		//	map[string]interface{}{"error": "fruit already exists"},
		//),
	}, tonic.Handler(getUser2, 200))

	userGrp.POST("/addUser", []fizz.OperationOption{
		fizz.Summary("添加一个用户"),
		//fizz.Response("400", "Bad request", nil, nil,
		//	map[string]interface{}{"error": "fruit already exists"},
		//),
	}, tonic.Handler(addUser, 200))

}

func getUser(c *gin.Context, u *UserParam) (render.RespData[entity.XxlJobUser], error) {
	fmt.Printf("-----------------user-------%+v", u)
	log := logger.New(context.Background(), initialize.GetZap())

	xxlJobUserDao := dao.GetDao("XxlJobUserDao").(*dao.XxlJobUserDao)

	jobUser := xxlJobUserDao.GetById(1)

	log.Info("jobUser:", jobUser)

	return render.OkResp[entity.XxlJobUser](jobUser), nil
}

func getUser2(c *gin.Context, u *UserParam2) (render.RespData[entity.XxlJobUser], error) {
	fmt.Printf("-----------------user-------%+v", u)
	log := logger.New(context.Background(), initialize.GetZap())

	xxlJobUserDao := dao.GetDao("XxlJobUserDao").(*dao.XxlJobUserDao)

	jobUser := xxlJobUserDao.GetById(1)

	log.Info("jobUser: ", jobUser)

	return render.OkResp[entity.XxlJobUser](jobUser), nil
}

/*
这里不用再写gin原生的 func(c *gin.Context) {} 了,直接写tonic定义的handler，handler形式如下：
func(*gin.Context, [input object ptr]) ([output object], error){}
其中 方括号的 是可选参数，默认请求方式是 application/json .入参会自动装配
返回值是可选的，返回的error为nil,表示成功
*/
func addUser(c *gin.Context, u *UserInfo) (render.RespData[*UserInfo], error) {
	log := logger.New(context.Background(), initialize.GetZap())
	log.Info("-----------------user-------{}", u)

	return render.OkResp(u), nil
}
